package com.hay.auth.service;

import com.hay.auth.client.UserClient;
import com.hay.auth.common.entity.UserInfo;
import com.hay.auth.common.utils.JwtUtils;
import com.hay.auth.config.JwtProperties;
import com.hay.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @title: AuthService
 * @Author HuangYan
 * @Date: 2020/8/4 15:10
 * @Version 1.0
 */
@Service
public class AuthService {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserClient userClient;

    public String accredit(String username, String password) {

        try {
            // 根据用户名和密码查询用户
            User user = userClient.queryUser(username, password);

            // 判断是否为空
            if (user == null){
                return null;
            }

            // 生成token
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setUsername(user.getUsername());
            String token = null;
            token = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
